package com.eqdd.yiqidian.ui.yiqixue.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.yiqidian.CoolRefreshRecyclerViewFragmentCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.TrainNeedListBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 18-2-7  下午2:12.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class FindNeedFragment extends BaseFragment {
    private CoolRefreshRecyclerViewFragmentCustom dataBinding;
    private int pageNum;
    private SlimAdapterEx slimAdapterEx;
    private String query;
    private MVCCoolHelper<List<TrainNeedListBean>> mvcHelper;

    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_cool_recycler_view, null, false);
    }

    @Override
    protected void initData() {
        if (slimAdapterEx == null) {
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.MONTH, -3);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            String url = getArguments().getString(Config.URL);
            String field = getArguments().getString(Config.FIELD);
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            //添加自定义分割线
            DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
            dataBinding.recyclerView.addItemDecoration(divider);
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_113, new SlimInjector<TrainNeedListBean>() {
                @Override
                public void onInject(TrainNeedListBean data, IViewInjector injector) {
                    injector.text(R.id.tv_one, "需求主题:" + data.getThetheme())
                            .text(R.id.tv_two, "关键字:" + data.getThecategory())
                            .gone(R.id.tv_three)
                            .text(R.id.tv_four, "创建时间:" + data.getCreateTime());
                }
            }).attachTo(dataBinding.recyclerView);
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                    });
            ModelRx2DataSource<TrainNeedListBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
                @Override
                public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                    pageNum = page == 1 ? 0 : pageNum;

                    return OkGo.<HttpResult<List<TrainNeedListBean>>>post(HttpConfig.BASE_URL + url)
                            .params("page", pageNum)
                            .params("para", query)
                            .params("date", sf.format(instance.getTime()))
                            .params("minBudget", 0)
                            .params("maxBudget", 999999)
                            .params("type", "")
                            .converter(new JsonConverter<HttpResult<List<TrainNeedListBean>>>() {
                                @Override
                                public void test() {
                                    super.test();
                                }
                            }).adapt(new FlowableBody<>())
                            .flatMap(listResultBean -> {
                                pageNum = listResultBean.getPage();
                                if (listResultBean.getStatus() == 200) {
                                    return Flowable.just(listResultBean.getItems() == null ? new ArrayList<TrainNeedListBean>() : listResultBean.getItems());
                                } else {
                                    return Flowable.just(new ArrayList<>());
                                }
                            });
                }
            });
            slimAdapterEx.updateData(new ArrayList<>());
            mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
            mvcHelper.setDataSource(dataSource);
            mvcHelper.setAdapter(slimAdapterEx);
            mvcHelper.refresh();
        }
    }

    @Override
    protected void setView() {

    }

    @Override
    public void onClick(View v) {

    }

    public void refresh(CharSequence currentQuery) {
        query = (String) currentQuery;
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

}
